package fxana;


import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class ParaFlag {
	protected boolean weight_mv=false;
	public static boolean save_plot=false;
	public static int plot_term=60;
	public  String option; /*used by ConditionEx*/
	protected File datafile=null;
	protected File datadir=null;
	protected Vector <Range >range=new Vector<Range>();
	
	protected Vector<Vector<Integer>>	allflags=new Vector<Vector<Integer>>();
	protected Vector<Integer>  aflag=new Vector<Integer>();
	protected Vector<Integer>  bflag=new Vector<Integer>();
	protected Vector<Integer>  cflag=new Vector<Integer>();
	protected Vector<Integer>  dflag=new Vector<Integer>();
	protected Vector<Integer>  eflag=new Vector<Integer>();
	protected Vector<Integer>  fflag=new Vector<Integer>();
	protected Vector<Integer>  gflag=new Vector<Integer>();
	protected Vector<Integer>  hflag=new Vector<Integer>();
	protected Vector<Integer>  iflag=new Vector<Integer>();
	protected Vector<Integer>  jflag=new Vector<Integer>();
	protected Vector<Integer>  kflag=new Vector<Integer>();
	protected Vector<Integer>  lflag=new Vector<Integer>();
	protected Vector<Integer>  mflag=new Vector<Integer>();
	protected Vector<Integer>  nflag=new Vector<Integer>();
	protected Vector<Integer>  oflag=new Vector<Integer>();
	protected Vector<Integer>  pflag=new Vector<Integer>();
	protected Vector<Integer>  qflag=new Vector<Integer>();
	protected Vector<Integer>  rflag=new Vector<Integer>();
	protected Vector<Integer>  sflag=new Vector<Integer>();
	protected Vector<Integer>  tflag=new Vector<Integer>();
	protected Vector<Integer>  uflag=new Vector<Integer>();
	protected Vector<Integer>  vflag=new Vector<Integer>();
	protected Vector<Integer>  wflag=new Vector<Integer>();
	protected Vector<Integer>  xflag=new Vector<Integer>();
	protected Vector<Integer>  yflag=new Vector<Integer>();
	protected Vector<Integer>  zflag=new Vector<Integer>();
	
	public ParaFlag(){
		allflags.add(aflag);
		allflags.add(bflag);
		allflags.add(cflag);
		allflags.add(dflag);
		allflags.add(eflag);
		allflags.add(fflag);
		allflags.add(gflag);
		allflags.add(hflag);
		allflags.add(iflag);
		allflags.add(jflag);
		allflags.add(kflag);
		allflags.add(lflag);
		allflags.add(mflag);
		allflags.add(nflag);
		allflags.add(oflag);
		allflags.add(pflag);
		allflags.add(qflag);
		allflags.add(rflag);
		allflags.add(sflag);
		allflags.add(tflag);
		allflags.add(uflag);
		allflags.add(vflag);
		allflags.add(wflag);
		allflags.add(xflag);
		allflags.add(yflag);
		allflags.add(zflag);
		return;
	}
	
	public void SetWeightMV(){
		weight_mv=true;
	}
	
	public void ClearWeightMV(){
		weight_mv=false;
	}
	
	public boolean IFWeightMV(){
		return weight_mv;
	}
	
	public void SetAFlag(int number){	aflag.add(new Integer(number));	}
	public void SetAFlag(){SetAFlag(-1);}
	public  Iterator<Integer>  GetAFlagIterator(){	return aflag.iterator();	}
	public void ClearAFlag(){ aflag.clear(); }
	public boolean IsAFlagSet() {
		if(aflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetBFlag(int number){	bflag.add(new Integer(number));	}
	public void SetBFlag(){SetBFlag(-1);}
	public  Iterator<Integer>  GetBFlagIterator(){	return bflag.iterator();	}
	public void ClearBFlag(){ bflag.clear(); }
	public boolean IsBFlagSet() {
		if(bflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetCFlag(int number){	cflag.add(new Integer(number));	}
	public void SetCFlag(){SetCFlag(-1);}
	public  Iterator<Integer>  GetCFlagIterator(){	return cflag.iterator();	}
	public void ClearCFlag(){ cflag.clear(); }
	public boolean IsCFlagSet() {
		if(cflag.isEmpty())
			return false;
		return true;	
	}

	public void SetDFlag(int number){	dflag.add(new Integer(number));	}
	public void SetDFlag(){SetDFlag(-1);}
	public  Iterator<Integer>  GetDFlagIterator(){	return dflag.iterator();	}
	public void ClearDFlag(){ dflag.clear(); }
	public boolean IsDFlagSet() {
		if(dflag.isEmpty())
			return false;
		return true;	
	}

	public void SetEFlag(int number){	eflag.add(new Integer(number));	}
	public void SetEFlag(){SetEFlag(-1);}
	public  Iterator<Integer>  GetEFlagIterator(){	return eflag.iterator();	}
	public void ClearEFlag(){ eflag.clear(); }
	public boolean IsEFlagSet() {
		if(eflag.isEmpty())
			return false;
		return true;	
	}

	public void SetFFlag(int number){	fflag.add(new Integer(number));	}
	public void SetFFlag(){SetFFlag(-1);}
	public  Iterator<Integer>  GetFFlagIterator(){	return fflag.iterator();	}
	public void ClearFFlag(){ fflag.clear(); }
	public boolean IsFFlagSet() {
		if(fflag.isEmpty())
			return false;
		return true;	
	}

	public void SetGFlag(int number){	gflag.add(new Integer(number));	}
	public void SetGFlag(){SetGFlag(-1);}
	public  Iterator<Integer>  GetGFlagIterator(){	return gflag.iterator();	}
	public void ClearGFlag(){ gflag.clear(); }
	public boolean IsGFlagSet() {
		if(gflag.isEmpty())
			return false;
		return true;	
	}

	public void SetHFlag(int number){	hflag.add(new Integer(number));	}
	public void SetHFlag(){SetHFlag(-1);}
	public  Iterator<Integer>  GetHFlagIterator(){	return hflag.iterator();	}
	public void ClearHFlag(){ hflag.clear(); }
	public boolean IsHFlagSet() {
		if(hflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetIFlag(int number){	iflag.add(new Integer(number));	}
	public void SetIFlag(){SetIFlag(-1);}
	public  Iterator<Integer>  GetIFlagIterator(){	return iflag.iterator();	}
	public void ClearIFlag(){ iflag.clear(); }
	public boolean IsIFlagSet() {
		if(iflag.isEmpty())
			return false;
		return true;	
	}

	public void SetJFlag(int number){	jflag.add(new Integer(number));	}
	public void SetJFlag(){SetJFlag(-1);}
	public  Iterator<Integer>  GetJFlagIterator(){	return jflag.iterator();	}
	public void ClearJFlag(){ jflag.clear(); }
	public boolean IsJFlagSet() {
		if(jflag.isEmpty())
			return false;
		return true;	
	}

	public void SetKFlag(int number){	kflag.add(new Integer(number));	}
	public void SetKFlag(){SetKFlag(-1);}
	public  Iterator<Integer>  GetKFlagIterator(){	return kflag.iterator();	}
	public void ClearKFlag(){ kflag.clear(); }
	public boolean IsKFlagSet() {
		if(kflag.isEmpty())
			return false;
		return true;	
	}

	public void SetLFlag(int number){	lflag.add(new Integer(number));	}
	public void SetLFlag(){SetLFlag(-1);}
	public  Iterator<Integer>  GetLFlagIterator(){	return lflag.iterator();	}
	public void ClearLFlag(){ lflag.clear(); }
	public boolean IsLFlagSet() {
		if(lflag.isEmpty())
			return false;
		return true;	
	}

	public void SetMFlag(int number){	mflag.add(new Integer(number));	}
	public void SetMFlag(){SetMFlag(-1);}
	public  Iterator<Integer>  GetMFlagIterator(){	return mflag.iterator();	}
	public void ClearMFlag(){ mflag.clear(); }
	public boolean IsMFlagSet() {
		if(mflag.isEmpty())
			return false;
		return true;	
	}

	public void SetNFlag(int number){	nflag.add(new Integer(number));	}
	public void SetNFlag(){SetNFlag(-1);}
	public  Iterator<Integer>  GetNFlagIterator(){	return nflag.iterator();	}
	public void ClearNFlag(){ nflag.clear(); }
	public boolean IsNFlagSet() {
		if(nflag.isEmpty())
			return false;
		return true;	
	}

	public void SetOFlag(int number){	oflag.add(new Integer(number));	}
	public void SetOFlag(){SetOFlag(-1);}
	public  Iterator<Integer>  GetOFlagIterator(){	return oflag.iterator();	}
	public void ClearOFlag(){ oflag.clear(); }
	public boolean IsOFlagSet() {
		if(oflag.isEmpty())
			return false;
		return true;	
	}

	public void SetPFlag(int number){	pflag.add(new Integer(number));	}
	public void SetPFlag(){SetPFlag(-1);}
	public  Iterator<Integer>  GetPFlagIterator(){	return pflag.iterator();	}
	public void ClearPFlag(){ pflag.clear(); }
	public boolean IsPFlagSet() {
		if(pflag.isEmpty())
			return false;
		return true;	
	}

	public void SetQFlag(int number){	qflag.add(new Integer(number));	}
	public void SetQFlag(){SetQFlag(-1);}
	public  Iterator<Integer>  GetQFlagIterator(){	return qflag.iterator();	}
	public void ClearQFlag(){ qflag.clear(); }
	public boolean IsQFlagSet() {
		if(qflag.isEmpty())
			return false;
		return true;	
	}

	public void SetRFlag(int number){	rflag.add(new Integer(number));	}
	public void SetRFlag(){SetRFlag(-1);}
	public  Iterator<Integer>  GetRFlagIterator(){	return rflag.iterator();	}
	public void ClearRFlag( ){ rflag.clear(); }
	public boolean IsRFlagSet() {
		if(rflag.isEmpty())
			return false;
		return true;	
	}

	public void SetSFlag(int number){	sflag.add(new Integer(number));	}
	public void SetSFlag(){SetSFlag(-1);}
	public  Iterator<Integer>  GetSFlagIterator(){	return sflag.iterator();	}
	public void ClearSFlag( ){ sflag.clear(); }
	public boolean IsSFlagSet() {
		if(sflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetTFlag(int number){	tflag.add(new Integer(number));	}
	public void SetTFlag(){SetTFlag(-1);}
	public  Iterator<Integer>  GetTFlagIterator(){	return tflag.iterator();	}
	public void ClearTFlag( ){ tflag.clear(); }
	public boolean IsTFlagSet() {
		if(tflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetUFlag(int number){	uflag.add(new Integer(number));	}
	public void SetUFlag(){SetUFlag(-1);}
	public  Iterator<Integer>  GetUFlagIterator(){	return uflag.iterator();	}
	public void ClearUFlag( ){ uflag.clear(); }
	public boolean IsUFlagSet() {
		if(uflag.isEmpty())
			return false;
		return true;	
	}

	public void SetVFlag(int number){	vflag.add(new Integer(number));	}
	public void SetVFlag(){SetVFlag(-1);}
	public  Iterator<Integer>  GetVFlagIterator(){	return vflag.iterator();	}
	public void ClearVFlag( ){ vflag.clear(); }
	public boolean IsVFlagSet() {
		if(vflag.isEmpty())
			return false;
		return true;	
	}

	public void SetWFlag(int number){	wflag.add(new Integer(number));	}
	public void SetWFlag(){SetWFlag(-1);}
	public  Iterator<Integer>  GetWFlagIterator(){	return wflag.iterator();	}
	public void ClearWFlag( ){ wflag.clear(); }
	public boolean IsWFlagSet() {
		if(wflag.isEmpty())
			return false;
		return true;	
	}
	
	public void SetXFlag(int number){	xflag.add(new Integer(number));	}
	public void SetXFlag(){SetXFlag(-1);}
	public  Iterator<Integer>  GetXFlagIterator(){	return xflag.iterator();	}
	public void ClearXFlag( ){ xflag.clear(); }
	public boolean IsXFlagSet() {
		if(xflag.isEmpty())
			return false;
		return true;	
	}

	public void SetYFlag(int number){	yflag.add(new Integer(number));	}
	public void SetYFlag(){SetYFlag(-1);}
	public  Iterator<Integer>  GetYFlagIterator(){	return yflag.iterator();	}
	public void ClearYFlag( ){ yflag.clear(); }
	public boolean IsYFlagSet() {
		if(yflag.isEmpty())
			return false;
		return true;	
	}

	public void SetZFlag(int number){	zflag.add(new Integer(number));	}
	public void SetZFlag(){SetZFlag(-1);}
	public  Iterator<Integer>  GetZFlagIterator(){	return zflag.iterator();	}
	public void ClearZFlag( ){ zflag.clear(); }
	public boolean IsZFlagSet() {
		if(zflag.isEmpty())
			return false;
		return true;	
	}
		
	public boolean need_process(){
		return this.IsOFlagSet()||this.IsAFlagSet()|| this.IsMFlagSet()|| this.IsRFlagSet()|| this.IsEFlagSet();
	}
	
	
	
public boolean IsParameterOK(){		
	
		if(IsHFlagSet()){
			return false;
		}
	
		if((datafile==null) && (datadir==null) ){
			System.err.println("Data parameter is not specified.");
			return false;
		}
		
		
		if(!IsAFlagSet() && !IsOFlagSet() && !IsMFlagSet() && ! IsEFlagSet()&& ! IsDFlagSet()&& ! IsLFlagSet()){
			
			if(IsPFlagSet()) {
				System.err.print("For -p/P, at least, one of '-o, -a, -m/M, -e, -d, -l' is needed !  clear following options: ");
				ClearPFlag(); System.out.println("-p/P");
			}
			
			if((!need_process()) && !IsBFlagSet()){
				return false;
			}
		}		
		
		return true;
	}

public void SetFlagByCmdline(String args[]){
	int iloop=0;		
	
	if(args.length==0){
		return;
	}
	
	for(iloop=0;iloop<args.length;iloop++){
		String tmp=args[iloop];

		if(tmp.startsWith("-")){
			tmp=tmp.substring(1);
			while(!tmp.isEmpty()){
													
				String regex="^[a-z][0-9]*";
				Pattern pattern=Pattern.compile(regex);
				Matcher matcher=pattern.matcher(tmp);
				if(matcher.find()){
					char flag=tmp.charAt(0);
					int number=-1;
					int next_index=1;

					String regex_2="^[a-z][0-9][0-9]*";
					Pattern pattern_2=Pattern.compile(regex_2);
					Matcher matcher_2=pattern_2.matcher(tmp);
					
					if(matcher_2.find()){
						number=Integer.valueOf(tmp.substring(1,matcher_2.end()));
						next_index=matcher_2.end();
					}			
					
					switch(flag){
					case 'a':  SetAFlag(number);break;
					case 'b':  SetBFlag(number);break;
					case 'c':  SetCFlag(number);break;
					case 'd':  SetDFlag(number);break;
					case 'e':  SetEFlag(number);break;
					case 'f':   SetFFlag(number);break;
					case 'g':  SetGFlag(number);break;
					case 'h':  SetHFlag(number);break;
					case 'i':   SetIFlag(number);break;
					case 'j':   SetJFlag(number);break;
					case 'k':  SetKFlag(number);break;
					case 'l':   SetLFlag(number);break;
					case 'm': SetMFlag(number);break;
					case 'n':  SetNFlag(number);break;
					case 'o':  SetOFlag(number);break;
					case 'p':  SetPFlag(number);break;
					case 'q':  SetQFlag(number);break;
					case 'r':   SetRFlag(number);break;
					case 's':   SetSFlag(number);break;
					case 't':   SetTFlag(number);break;
					case 'u':  SetUFlag(number);break;
					case 'v':   SetVFlag(number);break;
					case 'w':  SetWFlag(number);break;
					case 'x':  SetXFlag(number);break;
					case 'y':  SetYFlag(number);break;
					case 'z':  SetZFlag(number);break;
					default: System.out.println(flag+" is skipped");
					}
					
					tmp=tmp.substring(next_index);
					continue;						
				}
				
				System.err.print("Wrong parameter: "+tmp.charAt(0)+"...skipped"+"\n");	
				tmp=tmp.substring(1);
			}
			
			continue;
		}

		if(tmp.startsWith("[")){
			int start=1;
			int sep=tmp.indexOf(',');
			int end=tmp.indexOf(']');
			if(sep==-1|| end==-1){
				System.err.print("Wrong parameter: "+tmp+"...skipped"+"\n");
				continue;
			}

			Double num1,num2;
			try{
				num1=Double.valueOf(tmp.substring(start, sep));
				num2=Double.valueOf(tmp.substring(sep+1, end));
			}catch(NumberFormatException e){
				System.err.print("Wrong parameter: "+tmp+"...skipped"+"\n");
				continue;
			}
			range.add(new Range(num1,num2));
			if(end!=tmp.length()-1){
				System.err.print("Wrong parameter: "+tmp.substring(end+1)+"...skipped"+"\n");
			}
			continue;
		}

		if(tmp.startsWith("<")){
			int start=1;
			int sep=tmp.indexOf(',');
			int end=tmp.indexOf('>');
			if(sep==-1|| end==-1){
				System.err.print("Wrong parameter: "+tmp+"...skipped"+"\n");
				continue;
			}

			Date date1=DateCell.StringToDate(tmp.substring(start, sep));
			Date date2=DateCell.StringToDate(tmp.substring(sep+1, end));
			if(date1==null||date2==null){
				System.err.print("Wrong parameter: "+tmp+"...skipped"+"\n");
				continue;
			}

			range.add(new Range(date1,date2));
			if(end!=tmp.length()-1){
				System.err.print("Wrong parameter: "+tmp.substring(end+1)+"...skipped"+"\n");
			}
			continue;
		}

		datafile=new File(tmp);
		if(! datafile.exists() || !datafile.canRead()){
			System.err.print("Wrong data file/directory: "+tmp+"...skipped"+"\n");				
		}else{
			if(datafile.isDirectory()){
				datadir=datafile;
				datafile=null;
			}
		}
		
	}
}


	
	public String GetFilename(){
		if(datafile!=null){
			return datafile.getPath();
		}else{
			return null;
		}
	}
	
	public String GetDirname(){
		if(datadir!=null){
			return datadir.getPath();
		}else{
			return null;
		}
	}
	
	public String GetOptionString(){
		String optionstr="";
		int i;
		for(i=0;i<this.allflags.size();i++){
			Vector<Integer>tmp=(allflags.elementAt(i));
			if(tmp.isEmpty()){
				continue;
			}

			Character c;
			for(c='a';c<='z';c++){
				if(c-'a'==i){
					break;
				}
			}

			Iterator<Integer> it=tmp.iterator();
			while(it.hasNext()){
				optionstr+=Character.toUpperCase(c)+":"+it.next().toString()+" ";
			}
		}
		
		if(!this.range.isEmpty()){
			int ri;
			for(ri=0;ri<this.range.size();ri++){
				optionstr+=this.range.elementAt(ri).GetRangeString()+" ";
			}
		}

		return optionstr;
	}

	public void AddRange(Range rng){
		this.range.add(rng);
	}

	public Vector<Range> GetRangeSet(){
		return this.range;
	}

}
